package com.quyeying.service.impl;

import com.quyeying.entity.TblProductCategoryEntity;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.mapper.TblProductCategoryDao;
import com.quyeying.service.TblProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quyeying.service.TblProductService;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.stereotype.Service;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ReturnPageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import javax.annotation.Resource;

/**
* <p>
 * 商品分类 服务实现类
 * </p>
*
* @author skx
* @since 2022-10-18
*/
@Service
public class TblProductCategoryServiceImpl extends ServiceImpl<TblProductCategoryDao, TblProductCategoryEntity> implements TblProductCategoryService {

    @Resource
    private TblProductService tblProductService;

    @Override
    public R findListByPage(Integer page, Integer pageCount, Long parentId, Long id){
        IPage<TblProductCategoryEntity> wherePage = new Page<>(page, pageCount);
        TblProductCategoryEntity where = new TblProductCategoryEntity();
        where.setParentId(parentId);
        where.setMerchantId(id);
        IPage<TblProductCategoryEntity> iPage = baseMapper.selectPage(wherePage,  Wrappers.query(where).orderByAsc("sort").orderByDesc("update_time"));
        return R.success(ReturnPageData.fillingData(iPage));
    }

    @Override
    public R add(TblProductCategoryEntity tblProductCategoryEntity){
        baseMapper.insert(tblProductCategoryEntity);
        return R.success(tblProductCategoryEntity);
    }

    @Override
    public R delete(Long id){
        TblProductCategoryEntity tblProductCategoryEntity = baseMapper.selectById(id);
        boolean success;
        if(tblProductCategoryEntity.getParentId().equals(0L)) {
            //一级分类
            success = verifyIsNullData(id, false);
        }else {
            TblProductCategoryEntity productCategoryEntity = baseMapper.selectById(tblProductCategoryEntity.getParentId());
            success =  productCategoryEntity.getParentId().equals(0L) ? verifyIsNullData(id, false) : verifyIsNullData(id, true);
        }
        return success ? R.success(baseMapper.deleteById(id)) : R.error(ResultEnum.DATA_IS_NOT_NULL.getCode(), "当前分类不为空，无法删除！");
    }

    @Override
    public R updateData(TblProductCategoryEntity tblProductCategoryEntity){
        baseMapper.updateById(tblProductCategoryEntity);
        return R.success(tblProductCategoryEntity);
    }

    @Override
    public R findById(Long id){
        TblProductCategoryEntity tblProductCategoryEntity = baseMapper.selectById(id);
        return R.success(tblProductCategoryEntity);
    }


    /**
     * 验证分类下是否有数据
     * @return
     */
    public boolean verifyIsNullData (Long categoryId, Boolean isThree) {

        int count;
        if(!isThree) {
            TblProductCategoryEntity tblProductCategoryEntity = new TblProductCategoryEntity();
            tblProductCategoryEntity.setParentId(categoryId);
            count = baseMapper.selectCount(Wrappers.query(tblProductCategoryEntity));
        }else {
            TblProductEntity product = new TblProductEntity();
            product.setCategoryThreeId(categoryId);
            count = tblProductService.count(Wrappers.query(product));
        }
        return count == 0;
    }


}
